# 导包
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 创建浏览器对象并登录
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://localhost:8080/ecshop/")

# 定位登录并点击
login_ec = (By.PARTIAL_LINK_TEXT, "请登录")
login_tog = EC.presence_of_element_located(login_ec)(driver)
login_tog.click()

# 定位账号
accounts_input = (By.NAME, "username")
WebDriverWait(driver, 10).until(EC.presence_of_element_located(accounts_input)).send_keys("test")

# 定位密码输入框
password_loc = (By.NAME, "password")
WebDriverWait(driver, 10).until(EC.presence_of_element_located(password_loc)).send_keys("zy6282987")

# 定位立即登录
submit_loc = (By.XPATH, '//input["submit"]')
WebDriverWait(driver, 10).until(EC.presence_of_element_located(submit_loc)).click()
driver.implicitly_wait(10)

# 结果验证:定位首页左上角用户信息,检查有"test"字符串显示
try:
    result_loc = (By.CLASS_NAME, "f4_b")
    result = WebDriverWait(driver, 10).until(EC.presence_of_element_located(result_loc))
    print(result.text == "jerry")
except:
    print("登录失败")
# 关闭浏览器
driver.quit()

